package com.app.zenmewan.controller;

import com.app.zenmewan.commons.Act;
import com.app.zenmewan.entity.Spot;
import com.app.zenmewan.service.SpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Spot)表控制层
 *
 * @author makejava
 * @since 2020-05-03 19:52:56
 */
@RestController
@RequestMapping("spot")
public class SpotController {
    /**
     * 服务对象
     */
    @Resource
    private SpotService spotService;

    @Autowired
    private Act act;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Spot selectOne(Integer id) {
        return this.spotService.queryById(id);
    }
    @PostMapping("insert")
    public void insert(@RequestBody Spot spot[]){
        Spot s = new Spot();
        for (Spot spot1 : spot) {
            s.setSid(spot1.getSid());
            if(this.spotService.queryAll(s).size()==0){
                this.spotService.insert(spot1);
            }
        }
    }
    @GetMapping("tuijian/{sid}")
    public ModelAndView tuijian(@PathVariable("sid")int sid){
        List<Spot> list = this.act.getSpots(sid);
        if(list.size()>6){
            list.subList(0,6);
        }
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        modelAndView.addObject("list",list);
        return modelAndView;
    }

}